
//主题赛的算法
const gameZtCfg = require("./gameZtCfg");

// gameZtCfg.dwlvCfg 里面的数据结构如下
// var LevelConfig = {
//     dwType: number, //段位等级， 1 2 3 4 5 6 7
//     lvMin: number,  //最小等级，包含
//     lvMax: number,  //最大等级 包含
//     costXz: number, //升级消耗
//     reward: number, //升级的奖励
// }

class AlgorZt {
    static getInstance() {
        if (!AlgorZt.instance) {
            AlgorZt.instance = new AlgorZt();
            return AlgorZt.instance;
        } else {
            return AlgorZt.instance;
        }
    }

    //将配置转换成 一维数组
    getLvlistCfg(){
        let cfg = gameZtCfg.dwlvCfg;
        let arrCfg = {};
        for (const key in cfg) {
            let temp = cfg[key];
            for (let i = temp.lvMin; i <= temp.lvMax; i++) {
                arrCfg[i] = {
                    lv: i,
                    dwType:temp.dwType,
                    costXz: temp.costXz, //升级到下一级的消耗
                    reward:temp.reward,
                    rdType: temp.rdType, //奖励类型
                }
            }
        }
       return arrCfg;
    }

    //根据当前等级得到总的消耗
    getCurLvCost(lvCur){
        let totalCost = 0;
        let list = this.getLvlistCfg();
        for (let i = 1; i < lvCur; i++) {
            if(list[i]){
                totalCost += list[i].costXz;
            }
        }
        return totalCost;
    }
    /**
     * 根据勋章数量得到最新的等级
     * @param xzNum 当前勋章数量
     * @param lvCur 当前等级
     * @returns {number}
     */
    updateDwLv(xzNum){
        let res = {
            lv: 0,
            dwType: 0,
            reward: 0,
        }
        let list = this.getLvlistCfg();
        for (let key in list) {
            let temp = list[key];

            if(xzNum >= temp.costXz){
                xzNum -= temp.costXz
            }else{
                res.lv = temp.lv;
                res.dwType = temp.dwType;
                res.reward = temp.reward;
                res.rdType = temp.rdType;
                break;
            }
        }
        return res;
    }

}

module.exports = AlgorZt.getInstance();